using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace muduoDotNet.Tests
{
    public class BufferTest
    {
        [Fact]
        public void TestBufferAppendAndRead()
        {
            var buffer = new Buffer();
            
            var data = System.Text.Encoding.UTF8.GetBytes("Hello, World!");
            buffer.Append(data);
            
            Assert.Equal(data.Length, buffer.ReadableBytes);
            
            var readData = buffer.ReadAll();
            Assert.Equal(data, readData);
            Assert.Equal(0, buffer.ReadableBytes);
        }

        [Fact]
        public void TestBufferAppendString()
        {
            var buffer = new Buffer();
            
            var message = "Hello, 世界!";
            buffer.Append(message);
            
            var readMessage = buffer.ReadAllString();
            Assert.Equal(message, readMessage);
        }

        [Fact]
        public void TestBufferPrepend()
        {
            var buffer = new Buffer();
            
            // 先添加一些数据
            buffer.Append("World");
            
            // 然后前置数据
            var prependData = System.Text.Encoding.UTF8.GetBytes("Hello, ");
            buffer.Prepend(prependData);
            
            var result = buffer.ReadAllString();
            Assert.Equal("Hello, World", result);
        }

        [Fact]
        public void TestBufferSkip()
        {
            var buffer = new Buffer();
            
            buffer.Append("Hello, World!");
            
            // 跳过前7个字符
            buffer.Skip(7);
            
            var result = buffer.ReadAllString();
            Assert.Equal("World!", result);
        }

        [Fact]
        public void TestBufferClear()
        {
            var buffer = new Buffer();
            
            buffer.Append("Some data");
            Assert.True(buffer.ReadableBytes > 0);
            
            buffer.Clear();
            Assert.Equal(0, buffer.ReadableBytes);
        }

        [Fact]
        public void TestBufferAutoResize()
        {
            var buffer = new Buffer();
            
            // 添加大量数据以测试自动扩容
            var largeData = new byte[10000];
            for (int i = 0; i < largeData.Length; i++)
            {
                largeData[i] = (byte)(i % 256);
            }
            
            buffer.Append(largeData);
            Assert.Equal(largeData.Length, buffer.ReadableBytes);
            
            var readData = buffer.ReadAll();
            Assert.Equal(largeData, readData);
        }
    }
}